package it.tempogara;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class Launch extends javax.swing.JFrame {

    {
	// Set Look & Feel
	try {
	    javax.swing.UIManager.setLookAndFeel("com.jgoodies.looks.plastic.Plastic3DLookAndFeel");
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    private JPanel jPanel1;
    private JTextField jTextFieldF3;
    private JLabel jLabel1;
    private JTextField jTextFieldF2;
    private JLabel jLabelF3;
    private JLabel jLabelF2;
    private JLabel jLabelF1;
    private JLabel jLabelPF2;
    private JLabel jLabelPF1;
    private JButton jButtonFile;
    private JButton jButtonFile2;
    private JTextField jTextFieldPF1;
    private JButton jButtonRun;
    private JTextField jTextFieldPF2;
    private JTextField jTextFieldF1;

    private File[] files;
    private JButton jButtonGeneraF0;
    private JCheckBox jCheckBoxFemminile;
    private JCheckBox jCheckBoxMaschile;
    private JCheckBox jCheckoOrdinamento;
    private JCheckBox jCheckOrdinamentoComponenti;
    private File target;

    /**
     * Auto-generated main method to display this JFrame
     */
    public static void main(String[] args) {
	SwingUtilities.invokeLater(new Runnable() {
	    public void run() {
		Launch inst = new Launch();
		inst.setLocationRelativeTo(null);
		inst.setVisible(true);
	    }
	});
    }

    public Launch() {
	super();
	initGUI();
    }

    private void initGUI() {
	try {
	    {
		jPanel1 = new JPanel();
		getContentPane().add(jPanel1, BorderLayout.CENTER);
		jPanel1.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		jPanel1.setLayout(null);
		{
		    jButtonFile2 = new JButton();
		    jPanel1.add(jButtonFile2);
		    jButtonFile2.setText("Scegli cartella di destinazione");
		    jButtonFile2.setBounds(344, 305, 217, 23);
		    jButtonFile2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    JFileChooser inputFile = new JFileChooser();
			    inputFile.setMultiSelectionEnabled(false);
			    inputFile.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			    if (inputFile.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
				target = inputFile.getSelectedFile();
			    }
			}

		    });

		}
		{
		    jButtonFile = new JButton();
		    jPanel1.add(jButtonFile);
		    jButtonFile.setText("Carica file delle classifiche");
		    jButtonFile.setBounds(350, 63, 197, 23);
		    jButtonFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    JFileChooser inputFile = new JFileChooser();
			    inputFile.setMultiSelectionEnabled(true);
			    inputFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
			    if (inputFile.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
				files = inputFile.getSelectedFiles();
				jButtonRun.setEnabled(true);
			    }
			}

		    });
		}
		{
		    jLabelF1 = new JLabel();
		    jPanel1.add(jLabelF1);
		    jLabelF1.setText("Nome classification fascia 1");
		    jLabelF1.setBounds(172, 185, 172, 16);
		}
		{
		    jLabelF2 = new JLabel();
		    jPanel1.add(jLabelF2);
		    jLabelF2.setText("Nome classification fascia 2");
		    jLabelF2.setBounds(172, 225, 172, 16);
		}
		{
		    jLabelF3 = new JLabel();
		    jPanel1.add(jLabelF3);
		    jLabelF3.setText("Nome classification fascia 3");
		    jLabelF3.setBounds(172, 261, 172, 16);
		}
		{
		    jTextFieldF2 = new JTextField();
		    jPanel1.add(jTextFieldF2);
		    jTextFieldF2.setBounds(344, 222, 62, 23);
		}
		{
		    jTextFieldF3 = new JTextField();
		    jPanel1.add(jTextFieldF3);
		    jTextFieldF3.setBounds(344, 258, 62, 23);
		}
		{
		    jLabelPF1 = new JLabel();
		    jPanel1.add(jLabelPF1);
		    jLabelPF1.setText("Punteggio di partenza fascia 1");
		    jLabelPF1.setBounds(438, 185, 172, 16);
		}
		{
		    jLabelPF2 = new JLabel();
		    jPanel1.add(jLabelPF2);
		    jLabelPF2.setText("Punteggio di partenza fascia 2/3");
		    jLabelPF2.setBounds(438, 225, 184, 16);
		}
		{
		    jTextFieldF1 = new JTextField();
		    jPanel1.add(jTextFieldF1);
		    jTextFieldF1.setBounds(344, 182, 62, 23);
		}
		{
		    jTextFieldPF1 = new JTextField();
		    jPanel1.add(jTextFieldPF1);
		    jTextFieldPF1.setBounds(622, 182, 62, 23);
		}
		{
		    jTextFieldPF2 = new JTextField();
		    jPanel1.add(jTextFieldPF2);
		    jTextFieldPF2.setBounds(622, 222, 62, 23);
		}
		{
		    jButtonRun = new JButton();
		    jPanel1.add(jButtonRun);
		    jButtonRun.setText("Genera");
		    jButtonRun.setBounds(283, 316, 316, 55);
		    jButtonRun.setBounds(281, 454, 316, 55);
		    jButtonRun.setEnabled(false);
		    jButtonRun.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    String[] paths = new String[files.length];
			    for (int i = 0; i < files.length; i++)
				paths[i] = files[i].getAbsolutePath();
			    Engine engine = new Engine(new String[] { jTextFieldF1.getText(), jTextFieldF2.getText(), jTextFieldF3.getText() }, paths);
			    if (target != null)
				engine.setDestination(target.getAbsolutePath());
			    if (!jTextFieldPF1.getText().isEmpty())
				engine.setPuntPart1(Integer.valueOf(jTextFieldPF1.getText()));
			    if (!jTextFieldPF2.getText().isEmpty())
				engine.setPuntPart2(Integer.valueOf(jTextFieldPF2.getText()));
			    if (jCheckBoxMaschile.isSelected())
				engine.setMaschile(true);
			    if (jCheckBoxFemminile.isSelected())
				engine.setFemminile(true);
			    if (!jCheckBoxFemminile.isSelected() && !jCheckBoxMaschile.isSelected()) {
				engine.setMaschile(true);
				engine.setFemminile(true);
			    }
			    if (jCheckoOrdinamento.isSelected())
				engine.setOrdinamentoClassificati(true);
			    if (jCheckOrdinamentoComponenti.isSelected())
				engine.setOrdinamentoComponenti(true);
			    try {
				engine.run();
				JOptionPane.showMessageDialog(jPanel1, "Generazione completa");

			    } catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			    }
			}
		    });
		}
		{
		    jCheckoOrdinamento = new JCheckBox();
		    jPanel1.add(jCheckoOrdinamento);
		    jCheckoOrdinamento.setText("Ordina per numero classificati");
		    jCheckoOrdinamento.setBounds(340, 395, 217, 20);
		}
		{
		    jCheckBoxMaschile = new JCheckBox();
		    jPanel1.add(jCheckBoxMaschile);
		    jCheckBoxMaschile.setText("Maschile");
		    jCheckBoxMaschile.setBounds(450, 259, 83, 20);
		}
		{
		    jCheckBoxFemminile = new JCheckBox();
		    jPanel1.add(jCheckBoxFemminile);
		    jCheckBoxFemminile.setText("Femminile");
		    jCheckBoxFemminile.setBounds(593, 259, 91, 20);
		}
		{
		    jCheckOrdinamentoComponenti = new JCheckBox();
		    jPanel1.add(jCheckOrdinamentoComponenti);
		    jCheckOrdinamentoComponenti.setText("Priorit� al numero dei componenti");
		    jCheckOrdinamentoComponenti.setBounds(344, 354, 217, 20);
		}
		{
		    jLabel1 = new JLabel();
		    jPanel1.add(jLabel1);
		    jLabel1.setText("Senior/Promesse, Junior ed Allievi m/f (F0)");
		    jLabel1.setBounds(229, 111, 240, 16);
		}
		{
		    jButtonGeneraF0 = new JButton();
		    jPanel1.add(jButtonGeneraF0);
		    jButtonGeneraF0.setText("Genera");
		    jButtonGeneraF0.setBounds(509, 108, 113, 23);
		    jButtonGeneraF0.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    String[] paths = new String[files.length];
			    for (int i = 0; i < files.length; i++)
				paths[i] = files[i].getAbsolutePath();
			    Engine engine = new Engine(new String[] { jTextFieldF1.getText(), jTextFieldF2.getText(), jTextFieldF3.getText() }, paths);
			    if (target != null)
				engine.setDestination(target.getAbsolutePath());
			    if (!jTextFieldPF1.getText().isEmpty())
				engine.setPuntPart1(Integer.valueOf(jTextFieldPF1.getText()));
			    if (!jTextFieldPF2.getText().isEmpty())
				engine.setPuntPart2(Integer.valueOf(jTextFieldPF2.getText()));
			    if (jCheckBoxMaschile.isSelected())
				engine.setMaschile(true);
			    if (jCheckBoxFemminile.isSelected())
				engine.setFemminile(true);
			    if (!jCheckBoxFemminile.isSelected() && !jCheckBoxMaschile.isSelected()) {
				engine.setMaschile(true);
				engine.setFemminile(true);
			    }
			    if (jCheckoOrdinamento.isSelected())
				engine.setOrdinamentoClassificati(true);
			    if (jCheckOrdinamentoComponenti.isSelected())
				engine.setOrdinamentoComponenti(true);
			    try {
				engine.run();
				JOptionPane.showMessageDialog(jPanel1, "Generazione completa");

			    } catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			    }
			}
		    });
		}
	    }
	    {
	    }
	    this.setSize(900, 600);
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    public String actionProva() {
	return null;

    }

}
